package com.software.myopiachildren.ui;


import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.util.Util;
import com.inuker.bluetooth.library.connect.listener.BluetoothStateListener;
import com.software.myopiachildren.R;
import com.software.myopiachildren.base.BaseActivity;
import com.software.myopiachildren.content.Constant;
import com.software.myopiachildren.utils.ToastUtil;
import com.software.myopiachildren.utils.eb.EB;
import com.software.myopiachildren.utils.eb.EbData;
import com.software.myopiachildren.utils.sp.SP;
import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;

import org.greenrobot.eventbus.Subscribe;

import java.util.IllegalFormatCodePointException;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.software.myopiachildren.App.mBluetoothClient;


/**
 * 蓝牙连接页
 *
 * @author penglei
 * @date 2020/05/13
 */
public class BluetoothActivity extends BaseActivity {
    private static final String TAG = "BluetoothActivity";

    @BindView(R.id.iv_title_left)
    ImageView mIvTitleLeft;
    @BindView(R.id.tv_title_text)
    TextView mTvTitleText;
    @BindView(R.id.tv_title_right)
    TextView mTvTitleRight;

    @BindView(R.id.lay_title_home)
    LinearLayout mLayTitleHome;
    @BindView(R.id.iv_bluetooth_status)
    ImageView mIvBluetoothStatus;
    @BindView(R.id.tv_bluetooth_status)
    TextView mTvBluetoothStatus;
    @BindView(R.id.btn_bluetooth)
    Button mBtnBluetooth;

    private int isbluetooth = 1; // 1：未连接  2：已连接


    @Override
    protected int getContentViewId() {
        return R.layout.activity_bluetooth;
    }

    @Override
    public void initView() {
        EB.register(this);
        mTvTitleText.setText(getResources().getString(R.string.s_connect_bluetooth));
        mTvTitleRight.setText(getResources().getString(R.string.s_explanation));
        mTvTitleRight.setVisibility(View.VISIBLE);
    }

    @Override
    public void initData() {
        // 检测蓝牙是否打开
        isBluetoothOpened();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }


    @Override
    protected void onPause() {
        super.onPause();
    }


    @Subscribe
    public void onEvent(EbData data) {
        if (data.getType() == 21) {
            isBluetoothOpened();
        } else if (data.getType() == 31 || data.getType() == 32) {
            // 检测蓝牙是否打开
            isBluetoothOpened();
        }
    }


    @OnClick({R.id.iv_title_left, R.id.tv_title_right, R.id.btn_bluetooth})
    public void onClick(final View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;

            case R.id.tv_title_right:
                intent = new Intent(BluetoothActivity.this, ExplanationActivity.class);
                startActivity(intent);
                break;

            case R.id.btn_bluetooth:
                if (isbluetooth == 1) {
                    mBluetoothClient.openBluetooth();
                    mBluetoothClient.registerBluetoothStateListener(mBluetoothStateListener);
                } else if (isbluetooth == 2) {
                    mBluetoothClient.closeBluetooth();
                    mBluetoothClient.registerBluetoothStateListener(mBluetoothStateListener);
                }
                break;
        }
    }


    /**
     * 检测蓝牙是否打开
     */
    private void isBluetoothOpened() {
        boolean isBluetoothOpened = mBluetoothClient.isBluetoothOpened();
        if (isBluetoothOpened) {
            isbluetooth = 2;
            if (Util.isOnMainThread()) {
                Glide.with(getApplicationContext()).load(R.mipmap.ic_bluetooth_status_sussess).into(mIvBluetoothStatus);
            }
            mTvBluetoothStatus.setText(getResources().getString(R.string.s_onconnect));
            mBtnBluetooth.setText(getResources().getString(R.string.s_cancel_connect));
        } else {
            isbluetooth = 1;
            mTvBluetoothStatus.setText(getResources().getString(R.string.s_unconnect));
            if (Util.isOnMainThread()) {
                Glide.with(getApplicationContext()).load(R.mipmap.ic_bluetooth_status_failure).into(mIvBluetoothStatus);
            }
            mTvBluetoothStatus.setText(getResources().getString(R.string.s_unconnect));
            mBtnBluetooth.setText(getResources().getString(R.string.s_connect));
        }
    }


    private final BluetoothStateListener mBluetoothStateListener = new BluetoothStateListener() {
        @Override
        public void onBluetoothStateChanged(boolean openOrClosed) {
            if (openOrClosed) {
                Log.e(TAG, "====" + "打开蓝牙成功");
                EB.post(new EbData(34));
            } else {
                Log.e(TAG, "====" + "关闭蓝牙成功");
                EB.post(new EbData(33));
            }
            EB.post(new EbData(12));
            EB.post(new EbData(21));
//            isBluetoothOpened();
        }

    };


}
